package net.sk.china.city.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.util.IdUtil;
import net.sk.china.city.domain.dto.organization.OrganizationInfoDTO;
import net.sk.china.city.domain.dto.organization.OrganizationPairDTO;
import net.sk.china.city.domain.dto.organization.OrganizationSearchDTO;
import net.sk.china.city.domain.dto.organization.OrganizationUpdateDTO;
import net.sk.china.city.utils.ConvertUtil;
import com.github.pagehelper.PageInfo;
import net.sk.china.city.domain.dto.TableDataInfo;
import net.sk.china.city.service.IArCharitableOrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import net.sk.china.city.mapper.ArCharitableOrganizationMapper;
import net.sk.china.city.domain.ArCharitableOrganization;

/**
 * 慈善机构Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-07
 */
@Service
public class ArCharitableOrganizationServiceImpl implements IArCharitableOrganizationService
{
    @Autowired
    private ArCharitableOrganizationMapper arCharitableOrganizationMapper;

    /**
     * 查询慈善机构
     *
     * @param pkId 慈善机构主键
     * @return 慈善机构
     */
    @Override
    public OrganizationInfoDTO selectArCharitableOrganizationByPkId(Long pkId)
    {
        ArCharitableOrganization arCharitableOrganization = arCharitableOrganizationMapper.selectArCharitableOrganizationByPkId(pkId).orElse(new ArCharitableOrganization());
        return arCharitableOrganization.toOrganizationInfoDTO();
    }

    /**
     * 查询慈善机构列表
     *
     * @param organizationSearchDTO 慈善机构
     * @return 慈善机构
     */
    @Override
    public TableDataInfo<OrganizationInfoDTO> selectArCharitableOrganizationList(OrganizationSearchDTO organizationSearchDTO)
    {
        List<ArCharitableOrganization> arCharitableOrganizationList = arCharitableOrganizationMapper.selectArCharitableOrganizationList(organizationSearchDTO);
        PageInfo<ArCharitableOrganization> arCharitableOrganizationPageInfo = new PageInfo<>(arCharitableOrganizationList);
        List<OrganizationInfoDTO> organizationInfoDTOList = ConvertUtil.converList(arCharitableOrganizationList, ArCharitableOrganization::toOrganizationInfoDTO);
        return new TableDataInfo<>(organizationInfoDTOList, arCharitableOrganizationPageInfo.getTotal());
    }

    /**
     * 新增慈善机构
     * 
     * @param organizationUpdateDTO 慈善机构
     * @return 结果
     */
    @Override
    public int insertArCharitableOrganization(OrganizationUpdateDTO organizationUpdateDTO)
    {
        organizationUpdateDTO.setPkId(IdUtil.getSnowflakeNextId());
        return arCharitableOrganizationMapper.insertArCharitableOrganization(organizationUpdateDTO);
    }

    /**
     * 修改慈善机构
     * 
     * @param organizationUpdateDTO 慈善机构
     * @return 结果
     */
    @Override
    public int updateArCharitableOrganization(OrganizationUpdateDTO organizationUpdateDTO)
    {
        return arCharitableOrganizationMapper.updateArCharitableOrganization(organizationUpdateDTO);
    }

    /**
     * 批量删除慈善机构
     * 
     * @param pkIds 需要删除的慈善机构主键
     * @return 结果
     */
    @Override
    public int deleteArCharitableOrganizationByPkIds(Long[] pkIds)
    {
        return arCharitableOrganizationMapper.deleteArCharitableOrganizationByPkIds(pkIds);
    }

    /**
     * 删除慈善机构信息
     * 
     * @param pkId 慈善机构主键
     * @return 结果
     */
    @Override
    public int deleteArCharitableOrganizationByPkId(Long pkId)
    {
        return arCharitableOrganizationMapper.deleteArCharitableOrganizationByPkId(pkId);
    }

    @Override
    public List<OrganizationPairDTO> selectOrganizationList() {
        OrganizationSearchDTO organizationSearchDTO = new OrganizationSearchDTO();
        List<ArCharitableOrganization> arCharitableOrganizationList = arCharitableOrganizationMapper.selectArCharitableOrganizationList(organizationSearchDTO);
        return arCharitableOrganizationList.stream().map(arCharitableOrganization -> {
            OrganizationPairDTO organizationPairDTO = new OrganizationPairDTO();
            organizationPairDTO.setId(arCharitableOrganization.getPkId());
            organizationPairDTO.setName(arCharitableOrganization.getOrganizationName());
            return organizationPairDTO;
        }).collect(Collectors.toList());
    }
}
